# coding: utf8
import pandas as pd

from common import S_ACCOUNT


def _guess_usecols(filepath, *, columns):
    df = pd.read_csv(
        filepath, compression="zip", header=None, nrows=1
    )
    header = df.iloc[0].to_list()
    usecols = [header.index(column) for column in columns]
    return usecols


def read_csv(filepath, *, columns, dtype=None):
    usecols = _guess_usecols(
        filepath=filepath, columns=columns
    )

    df = pd.read_csv(
        filepath,
        compression="zip",
        usecols=usecols,
        skiprows=1,
        header=None,
        names=columns,
        dtype="string" if dtype is None else dtype,
    )

    return df


def read_excel(filepath):
    df = pd.read_excel(
        filepath,
        names=[S_ACCOUNT],
        skiprows=1,
        header=None,
        dtype={S_ACCOUNT: "string"},
    )
    return df[S_ACCOUNT]
